/**
 * 核心技术支持
 * IT渠道网 小程序商城
 * 13873174181 微信同号
 */
import {Address} from '../../utils/address.js';
var address = new Address();

Page({
    data: {
        addressList: []
    },
    /**
     * 获取收货地址,绑定
     */
    onShow: function() {
        var that = this;
        address.getAddress((res)=> {
            that.setData({
                addressList: res
            })
        })
    },
    /**
     * 选中地址返回
     */
    pickAddress: function(event) {
        var i = address.getDataSet(event,'index');
        address.execSetStorageSync(this.data.addressList[i]);
        wx.navigateBack({
            delta: 1
        })
    },
    /**
     * 一键获取用户地址,存储地址
     */
    getWechatAddress: function() {
        var that = this;
        wx.chooseAddress({
            success: function (res) {
                var addressInfo = {
                    name: res.userName,
                    mobile: res.telNumber,
                    totalDetail: address.setAddressInfo(res)
                }
                if(res.telNumber){
                    // 加入地址列表
                    that.data.addressList.push(addressInfo);
                    that.setData({
                        addressList: that.data.addressList
                    })
                    // 存储地址
                    address.submitAddress(res, 0, (flag)=> {
                        if(!flag){
                            that.showTips('操作提示', '地址信息更新失败！')
                        }
                    })
                }else{
                    that.showTips('操作提示', '地址信息更新失败,手机号码为空！')
                }
            }
        })
    }
})